<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/03/07 13:45:35 $)
// $RCSfile: mobile_initexit.php,v $ : $Revision: 1.8 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* ###############################################
* EXECUTED WHEN STAFF FIRST LOGS IN
* ###############################################
*/
if ($eventaction == "mobileLogin")
{
	global $xml, $datastore;

	$datastore->cache("prioritycache");
	$datastore->cache("statuscache");
	$datastore->cache("staffcache");
	$datastore->cache("departmentcache");
	$datastore->cache("slaplancache");
	$datastore->cache("slaschedulecache");
	$datastore->cache("escalationrulecache");
	$datastore->cache("attachmenttypescache");
	$datastore->cache("ticketcountcache");
	$datastore->cache("ticketgridcache");
	$datastore->cache("ticketviewcache");
	$datastore->cache("queuecache");
	$datastore->cache("instaalertcache");
	$datastore->cache("ticketlabelcache");
	$datastore->cache("labelcountcache");
	$datastore->loadData();

	require_once ("./includes/functions_jscache.php");
	require_once ("./modules/tickets/functions_ticketsui.php");
	require_once ("./modules/tickets/functions_xmlapi.php");
	require_once ("./modules/tickets/functions_mobile.php");
	require_once ("./modules/tickets/functions_predefined.php");

	$departmentidlist = getAssignedDepartments(MODULE_TICKETS);

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."departments` ORDER BY `displayorder` ASC;");
	while ($dbCore->nextRecord())
	{
		if ($dbCore->Record["departmentmodule"] == MODULE_TICKETS)
		{
			$xml->addParentTag("department");
				$xml->addTag("departmentid", $dbCore->Record["departmentid"]);
				$xml->addTag("title", $dbCore->Record["title"]);
				$xml->addTag("isassigned", iif(in_array($dbCore->Record["departmentid"], $departmentidlist), "1", "0"));
			$xml->endTag("department");
		}
	}

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."ticketstatus` ORDER BY `displayorder` ASC;");
	while ($dbCore->nextRecord())
	{
		$xml->addParentTag("status");
			$xml->addTag("ticketstatusid", $dbCore->Record["ticketstatusid"]);
			$xml->addTag("title", $dbCore->Record["title"]);
			$xml->addTag("statuscolor", getMobileColorCode($dbCore->Record["statuscolor"]));
		$xml->endTag("status");
	}

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."ticketpriorities` ORDER BY `displayorder` ASC;");
	while ($dbCore->nextRecord())
	{
		$xml->addParentTag("priority");
			$xml->addTag("priorityid", $dbCore->Record["priorityid"]);
			$xml->addTag("title", $dbCore->Record["title"]);
			$xml->addTag("prioritycolor", getMobileColorCode($dbCore->Record["frcolorcode"]));
		$xml->endTag("priority");
	}

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."staff` ORDER BY `fullname` ASC;");
	while ($dbCore->nextRecord())
	{
		$xml->addParentTag("staff");
			$xml->addTag("staffid", $dbCore->Record["staffid"]);
			$xml->addTag("fullname", $dbCore->Record["fullname"]);
			$xml->addTag("email", $dbCore->Record["email"]);
		$xml->endTag("staff");
	}

	$xml->addParentTag("tree");
		renderXMLTree($xml, $departmentidlist, true);
	$xml->endTag("tree");


	// 1st - Queue Emails
	if (is_array($_SWIFT["queuecache"]["list"]))
	{
		foreach ($_SWIFT["queuecache"]["list"] as $key=>$val)
		{
			$fromname = iif(empty($val["customfromname"]), $_SWIFT["staff"]["fullname"], $val["customfromname"]);
			$fromemail = iif(empty($val["customfromemail"]), $val["email"], $val["customfromemail"]);

			$xml->addParentTag("fromqueue");
				$xml->addTag("emailqueueid", $val["emailqueueid"]);
				$xml->addTag("fullname", $fromname);
				$xml->addTag("email", $fromemail);
				$xml->addTag("departmentid", $val["departmentid"]);
			$xml->endTag("fromqueue");
		}
	}

	// Default Return Email
	$xml->addParentTag("fromqueue");
		$xml->addTag("emailqueueid", "-1");
		$xml->addTag("fullname", $_SWIFT["staff"]["fullname"]);
		$xml->addTag("email", $_SWIFT["settings"]["general_returnemail"]);
		$xml->addTag("departmentid", 0);
	$xml->endTag("fromqueue");

	// Staff Personal Email
	if ($_SWIFT["settings"]["general_returnemail"] != $_SWIFT["staff"]["email"])
	{
		$xml->addParentTag("fromqueue");
			$xml->addTag("emailqueueid", "0");
			$xml->addTag("fullname", $_SWIFT["staff"]["fullname"]);
			$xml->addTag("email", $_SWIFT["staff"]["email"]);
			$xml->addTag("departmentid", 0);
		$xml->endTag("fromqueue");
	}

	buildPredefinedXML($xml);

	$session->flushInactiveSessions();


/**
* ###############################################
* EXECUTED WHEN STAFF LOGS OUT
* ###############################################
*/
} else if ($eventaction == "mobileLogout") {
	$session->flushInactiveSessions();
}
?>